﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class TVR02
    {

        public TVR02()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VVRKOPF();
            DataContext = MV;
            AUFTYP = "K";
            InitializeComponent();
        }

        public override void OnAppModeChg()
        {
            base.OnAppModeChg();
            if (KRS.Length > 1)
                return;
            switch (app.company)
            {
                case "TST":
                    KRS = MV.year.Substring(2, 2);
                    switch (app.AppMode)
                    {
                        case "TISSUE":
                            //KRS = "S" + MV.year.Substring(3, 1);
                            break;
                        case "TMODE":
                            //KRS = "M" + MV.year.Substring(3, 1);
                            break;

                    }
                    break;
            }
        }



        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                KUNDNR = "";
                VVKP = "";
                KEY = "";
            }
            else
            {
                KEY = ((VVRKOPF)MV).VRKRENR;
                KUNDNR = ((VVRKOPF)MV).VRKKUNDNR;
                VVKP = ((VVRKOPF)MV).VRKZESSION + ((VVRKOPF)MV).VRKVERBNR;
            }
        }

        //待处理的尾款
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            if (LFD == "000000") LFD = "";
            dwData["VRKMANDANT"] = app.Mandant;
            dwData["VRKZESSION"] = " AND VRKZESSION='" + AUFTYP + "'";
            dwData["VRKZAHLBED"] = " AND VRKZAHLBED LIKE '" + ZAHLBED + "%'";
            dwData["VRKVERBNR"] = " AND VRPAUFKRS LIKE '%" + KRS + "%'";
            dwData["VRKMWSTID"] = " AND VRPAUFLFD LIKE '%" + LFD + "%'"; 
            SResult = TMV.QVRK04(dwData);
            MergeData(SResult, true, true);
            base.QueryCommand(sender, e);
        }
        private void AUF_Enter(object sender, System.Windows.Input.KeyEventArgs e)
        {

            if (e.Key != System.Windows.Input.Key.Enter)
                return;
            if (e.Key != System.Windows.Input.Key.Return)
                return;
            QueryCommand(null, null);
        }
        //预付款
        private void VKKPaymentCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            if (LFD == "000000") LFD = "";
            dwData["VRKMANDANT"] = app.Mandant;
            dwData["VRKZESSION"] = " AND VRKZESSION='" + AUFTYP + "'";
            dwData["VRKZAHLBED"] = " AND VRKZAHLBED LIKE '" + ZAHLBED + "%'";
            dwData["VRKVERBNR"] = " AND VRPAUFKRS LIKE '%" + KRS + "%'";
            dwData["VRKMWSTID"] = " AND VRPAUFLFD LIKE '%" + LFD + "%'";
            string filter = "AND VRPZOLLTNR LIKE '1:%'";
            if (KUNDNR.Length > 3)
            {
                filter += "  AND VRPARTKUNDNR='" + KUNDNR + "'";
            }
            else
            {
                filter += "   AND VRPARTKUNDNR<>'00000000'";
            }
            if (!IsPaid)
            {
                filter += "  AND VRPBONKZ='N'";
            }
            dwData["VRKFILLER"] = filter;
            SResult = TMV.QVRK04(dwData);
            MergeData(SResult, true, true);
            base.QueryCommand(sender, e);
        }
        //中款
        private void VVPaymentCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            if (LFD == "000000") LFD = "";
            dwData["VRKMANDANT"] = app.Mandant;
            dwData["VRKZESSION"] = " AND VRKZESSION='" + AUFTYP + "'";
            dwData["VRKZAHLBED"] = " AND VRKZAHLBED LIKE '" + ZAHLBED + "%'";
            dwData["VRKVERBNR"] = " AND VRPAUFKRS LIKE '%" + KRS + "%'";
            dwData["VRKMWSTID"] = " AND VRPAUFLFD LIKE '%" + LFD + "%'";
            string filter = "AND VRPZOLLTNR LIKE '2:%'";
            if (KUNDNR.Length > 3)
            {
                filter += "  AND VRPARTKUNDNR='" + KUNDNR + "'";
            }
            else
            {
                filter += "   AND VRPARTKUNDNR<>'00000000'";
            }
            if (!IsPaid)
            {
                filter += "  AND VRPBONKZ='N'";
            }
            dwData["VRKFILLER"] = filter;
            SResult = TMV.QVRK04(dwData);
            MergeData(SResult, true, true);
            base.QueryCommand(sender, e);
        }
        //尾款
        private void RESTPaymentCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            if (LFD == "000000") LFD = "";
            dwData["VRKMANDANT"] = app.Mandant;
            dwData["VRKZESSION"] = " AND VRKZESSION='" + AUFTYP + "'";
            dwData["VRKZAHLBED"] = " AND VRKZAHLBED LIKE '" + ZAHLBED + "%'";
            dwData["VRKVERBNR"] = " AND VRPAUFKRS LIKE '%" + KRS + "%'";
            dwData["VRKMWSTID"] = " AND VRPAUFLFD LIKE '%" + LFD + "%'";
            string filter = "AND VRPZOLLTNR LIKE '3:%'";
            if (KUNDNR.Length > 3)
            {
                filter += "  AND VRPARTKUNDNR='" + KUNDNR + "'";
            }
            else
            {
                filter += "   AND VRPARTKUNDNR<>'00000000'";
            }
            if (!IsPaid)
            {
                filter += "  AND VRPBONKZ='N'";
            }
            dwData["VRKFILLER"] = filter;

            SResult = TMV.QVRK04(dwData);
            MergeData(SResult, true, true);
            base.QueryCommand(sender, e);
        }

        private void OpenVRCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            if (LFD == "000000") LFD = "";
            dwData["VRKMANDANT"] = app.Mandant;
            dwData["VRKZESSION"] = " AND VRKZESSION='" + AUFTYP + "'";
            dwData["VRKZAHLBED"] = " AND VRKZAHLBED LIKE '" + ZAHLBED + "%'";
            dwData["VRKVERBNR"] = " AND VRPAUFKRS LIKE '%" + KRS + "%'";
            dwData["VRKMWSTID"] = " AND VRPAUFLFD LIKE '%" + LFD + "%'"; 
            string filter = "AND  SUBSTR(VRPZOLLTNR,1,2)<>'0:' ";
            if (KUNDNR.Length > 3)
            {
                filter += "  AND VRPARTKUNDNR='" + KUNDNR + "'";
            }
            else
            {
                filter += "   AND VRPARTKUNDNR<>'00000000'";
            }
            if (!IsPaid)
            {
                filter += "  AND VRPBONKZ='N'";
            }
            dwData["VRKFILLER"] = filter;

            SResult = TMV.QVRK04(dwData);
            MergeData(SResult, true, true);
            base.QueryCommand(sender, e);
        }

        public void CommissionCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
            {
                MessageBox.Show("请选择一个发票行");
                return;
            }
            PAYPAL = 9;
            PM["RENR"] = KEY;
            PM["PAYPAL"] = PAYPAL;
            PM["ORDERNUM"] = VVKP;
            PM["KRS"] = dwMaster["VRPAUFKRS"].ToString();
            PM["LFD"] = dwMaster["VRPAUFLFD"].ToString();
            PM["ZAHLBED"] = ZAHLBED;
            PM["AUFTYP"] = "K";
            PM["KUNDNR"] = KUNDNR;
            TMV.VRPCM(PM);
        }
        public void BonusCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
            {
                MessageBox.Show("请选择一个发票行");
                return;
            }
            PAYPAL = 9;
            PM["RENR"] = KEY;
            PM["PAYPAL"] = PAYPAL;
            PM["ORDERNUM"] = VVKP;
            PM["KRS"] = dwMaster["VRPAUFKRS"].ToString();
            PM["LFD"] = dwMaster["VRPAUFLFD"].ToString();
            PM["ZAHLBED"] = ZAHLBED;
            PM["AUFTYP"] = "K";
            PM["KUNDNR"] = KUNDNR;
            TMV.VRPBN(PM);
        }

    }
}
